package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.Exame;

import com.model.Rainhas;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table RAINHAS.
*/
public class RainhasDao extends AbstractDao<Rainhas, Long> {

    public static final String TABLENAME = "RAINHAS";

    /**
     * Properties of entity Rainhas.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idTrocRainhas");
        public final static Property Descricao = new Property(1, String.class, "descricao", false, "DESCRICAO");
        public final static Property NumeroColmeia = new Property(2, String.class, "NumeroColmeia", false, "NUMERO_COLMEIA");
        public final static Property Qualidadepostura = new Property(3, Integer.class, "qualidadepostura", false, "QUALIDADEPOSTURA");
        public final static Property Dataintroducao = new Property(4, String.class, "dataintroducao", false, "DATAINTRODUCAO");
        public final static Property Obs = new Property(5, String.class, "Obs", false, "OBS");
        public final static Property ApicultorId = new Property(6, long.class, "apicultorId", false, "APICULTOR_ID");
        public final static Property ApiarioId = new Property(7, long.class, "apiarioId", false, "APIARIO_ID");
        public final static Property ExameId = new Property(8, long.class, "exameId", false, "EXAME_ID");
        public final static Property Date = new Property(9, java.util.Date.class, "date", false, "DATE");
        public final static Property ApiarioOrigemId = new Property(10, long.class, "apiarioOrigemId", false, "APIARIO_ORIGEM_ID");
    };

    private DaoSession daoSession;

    private Query<Rainhas> apiario_ApiarioRainhasQuery;
    private Query<Rainhas> apicultor_ApicultorRainhasQuery;
    private Query<Rainhas> exame_ExameTrainhasQuery;
    private Query<Rainhas> apiario_ApiarioOriRainhasQuery;

    public RainhasDao(DaoConfig config) {
        super(config);
    }
    
    public RainhasDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'RAINHAS' (" + //
                "'_idTrocRainhas' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'DESCRICAO' TEXT," + // 1: descricao
                "'NUMERO_COLMEIA' TEXT," + // 2: NumeroColmeia
                "'QUALIDADEPOSTURA' INTEGER," + // 3: qualidadepostura
                "'DATAINTRODUCAO' TEXT," + // 4: dataintroducao
                "'OBS' TEXT," + // 5: Obs
                "'APICULTOR_ID' INTEGER NOT NULL ," + // 6: apicultorId
                "'APIARIO_ID' INTEGER NOT NULL ," + // 7: apiarioId
                "'EXAME_ID' INTEGER NOT NULL ," + // 8: exameId
                "'DATE' INTEGER," + // 9: date
                "'APIARIO_ORIGEM_ID' INTEGER NOT NULL );"); // 10: apiarioOrigemId
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'RAINHAS'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Rainhas entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        String descricao = entity.getDescricao();
        if (descricao != null) {
            stmt.bindString(2, descricao);
        }
 
        String NumeroColmeia = entity.getNumeroColmeia();
        if (NumeroColmeia != null) {
            stmt.bindString(3, NumeroColmeia);
        }
 
        Integer qualidadepostura = entity.getQualidadepostura();
        if (qualidadepostura != null) {
            stmt.bindLong(4, qualidadepostura);
        }
 
        String dataintroducao = entity.getDataintroducao();
        if (dataintroducao != null) {
            stmt.bindString(5, dataintroducao);
        }
 
        String Obs = entity.getObs();
        if (Obs != null) {
            stmt.bindString(6, Obs);
        }
        stmt.bindLong(7, entity.getApicultorId());
        stmt.bindLong(8, entity.getApiarioId());
        stmt.bindLong(9, entity.getExameId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(10, date.getTime());
        }
    }

    @Override
    protected void attachEntity(Rainhas entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Rainhas readEntity(Cursor cursor, int offset) {
        Rainhas entity = new Rainhas( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // descricao
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // NumeroColmeia
            cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3), // qualidadepostura
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // dataintroducao
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // Obs
            cursor.getLong(offset + 6), // apicultorId
            cursor.getLong(offset + 7), // apiarioId
            cursor.getLong(offset + 8), // exameId
            cursor.isNull(offset + 9) ? null : new java.util.Date(cursor.getLong(offset + 9)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Rainhas entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setDescricao(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setNumeroColmeia(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setQualidadepostura(cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3));
        entity.setDataintroducao(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setObs(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setApicultorId(cursor.getLong(offset + 6));
        entity.setApiarioId(cursor.getLong(offset + 7));
        entity.setExameId(cursor.getLong(offset + 8));
        entity.setDate(cursor.isNull(offset + 9) ? null : new java.util.Date(cursor.getLong(offset + 9)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Rainhas entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Rainhas entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioRainhas" to-many relationship of Apiario. */
    public List<Rainhas> _queryApiario_ApiarioRainhas(long apiarioId) {
        synchronized (this) {
            if (apiario_ApiarioRainhasQuery == null) {
                QueryBuilder<Rainhas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_ApiarioRainhasQuery = queryBuilder.build();
            }
        }
        Query<Rainhas> query = apiario_ApiarioRainhasQuery.forCurrentThread();
        query.setParameter(0, apiarioId);
        return query.list();
    }

    /** Internal query to resolve the "apicultorRainhas" to-many relationship of Apicultor. */
    public List<Rainhas> _queryApicultor_ApicultorRainhas(long apicultorId) {
        synchronized (this) {
            if (apicultor_ApicultorRainhasQuery == null) {
                QueryBuilder<Rainhas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApicultorId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_ApicultorRainhasQuery = queryBuilder.build();
            }
        }
        Query<Rainhas> query = apicultor_ApicultorRainhasQuery.forCurrentThread();
        query.setParameter(0, apicultorId);
        return query.list();
    }

    /** Internal query to resolve the "exameTrainhas" to-many relationship of Exame. */
    public List<Rainhas> _queryExame_ExameTrainhas(long exameId) {
        synchronized (this) {
            if (exame_ExameTrainhasQuery == null) {
                QueryBuilder<Rainhas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ExameId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                exame_ExameTrainhasQuery = queryBuilder.build();
            }
        }
        Query<Rainhas> query = exame_ExameTrainhasQuery.forCurrentThread();
        query.setParameter(0, exameId);
        return query.list();
    }

    /** Internal query to resolve the "apiarioOriRainhas" to-many relationship of Apiario. */
    public List<Rainhas> _queryApiario_ApiarioOriRainhas(long apiarioOrigemId) {
        synchronized (this) {
            if (apiario_ApiarioOriRainhasQuery == null) {
                QueryBuilder<Rainhas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioOrigemId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_ApiarioOriRainhasQuery = queryBuilder.build();
            }
        }
        Query<Rainhas> query = apiario_ApiarioOriRainhasQuery.forCurrentThread();
        query.setParameter(0, apiarioOrigemId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getExameDao().getAllColumns());
            builder.append(" FROM RAINHAS T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'APIARIO_ID'=T0.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T1 ON T.'APICULTOR_ID'=T1.'_idApicultor'");
            builder.append(" LEFT JOIN EXAME T2 ON T.'EXAME_ID'=T2.'_idExame'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Rainhas loadCurrentDeep(Cursor cursor, boolean lock) {
        Rainhas entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }

        return entity;    
    }

    public Rainhas loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Rainhas> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Rainhas> list = new ArrayList<Rainhas>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Rainhas> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Rainhas> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
